package com.mnt.washio.adapters;

import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.mnt.washio.R;
import com.mnt.washio.objects.Card;
import com.mnt.washio.views.CustomFontTextView;

public abstract class PaymentListAdapter extends ArrayAdapter<Card> {
	private DecimalFormat decimalFormat = new DecimalFormat("00");

	public PaymentListAdapter(Context context, List<Card> cards) {
		super(context, R.layout.item_list_payment, cards);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Card c = getItem(position);
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) getContext()
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.item_list_payment, parent,
					false);
		}

		CustomFontTextView txtTitle = (CustomFontTextView) convertView
				.findViewById(R.id.tv1);
		CustomFontTextView txtExpire = (CustomFontTextView) convertView
				.findViewById(R.id.tv2);

		String subNumber = c.getLast4();
		if (subNumber.length() > 0) {
			subNumber = subNumber.substring(subNumber.length() - 4);
		}
		txtTitle.setText(c.getType() + " " + subNumber);
		txtExpire.setText(decimalFormat.format(c.getExpMonth()) + "/"
				+ c.getExpYear());

		ImageView imvDelete = (ImageView) convertView
				.findViewById(R.id.imv_payment_delete);
		imvDelete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				new AlertDialog.Builder(getContext())
						.setTitle("Delete Credit Card")
						.setMessage("Are you sure you want to delete this credit card?")
						.setPositiveButton("Ok",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										onRemoveCard(getItem(position),
												position);
									}
								})
						.setNegativeButton("Cancel",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}
								}).show();
			}
		});

		return convertView;
	}

	public abstract void onRemoveCard(Card card, int position);
}
